package com.hzit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.entity.PageResult;
import com.hzit.entity.Specification;
import com.hzit.entity.SpecificationOption;
import com.hzit.entity.group.SpecGroup;
import com.hzit.mapper.SpecificationMapper;
import com.hzit.mapper.SpecificationOptionMapper;
import com.hzit.service.SpecificationService;
import com.hzit.vo.SpecificationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {
	@Autowired
	private SpecificationMapper specificationMapper;
	@Autowired
	private SpecificationOptionMapper optionMapper;

	//1. 查询所有的规格列表
	@Override
	public List<Specification> findAll() {
		return specificationMapper.selectAll();
	}

	//2. 分页查询规格列表
	@Override
	public PageResult<Specification> findByPage(Integer page, Integer pageSize) {
		//2.1 开始分页
		PageHelper.startPage(page,pageSize);
		//2.2 开始查询
		List<Specification> specifications = specificationMapper.selectAll();
		//2.3 转换为Page<T>对象
		Page<Specification> specificationPage = (Page<Specification>) specifications;
		//2.4 返回分页结果对象
		return new PageResult<>(specificationPage.getTotal(),specificationPage.getResult());
	}

	//3. 条件查询带分页
	@Override
	public PageResult<Specification> search(Integer page, Integer pageSize, SpecificationVo vo) {
		//3.1 开始分页
		PageHelper.startPage(page,pageSize);
		//3.2 开始条件查询
		Example example = new Example(Specification.class);
		Example.Criteria criteria = example.createCriteria();
		//3.3 开始封装查询条件
		if(vo != null){
			if(StrUtil.isNotBlank(vo.getSpecName())){
				criteria.andLike("specName","%" + vo.getSpecName() + "%");
			}
		}
		//3.4 开始进行条件查询
		List<Specification> specifications = specificationMapper.selectByExample(example);
		//3.5 转换为Page<Specification>对象
		Page<Specification> specificationPage = (Page<Specification>) specifications;
		//3.6 返回
		return new PageResult<>(specificationPage.getTotal(),specificationPage.getResult());
	}

	//4. 根据规格id查询规格名及规格选项列表
	@Override
	public SpecGroup findById(Long id) {
		//4.1 根据id查询规格对象
		Specification specification = specificationMapper.selectByPrimaryKey(id);
		//4.2 根据规格的id在tb_specification_option表中查询中规格选项列表
		Example example = new Example(SpecificationOption.class);
		Example.Criteria criteria = example.createCriteria();
		if(id != null){
			criteria.andEqualTo("specId",id);
		}
		//4.3 根据规格id查询规格选项列表
		List<SpecificationOption> options = optionMapper.selectByExample(example);
		//4.4 定义组合对象
		SpecGroup group = new SpecGroup(id,specification.getSpecName(),options);
		//4.5 最后，返回
		return group;
	}

	//5. 添加规格及规格选项列表
	@Override
	public void add(SpecGroup group) {
		//5.1 首先，添加规格
		String specName = group.getSpecName();
		Specification specification = new Specification();
		specification.setSpecName(specName);
		specificationMapper.insert(specification);
		//5.2 得到添加后的主键值
		Long id = specification.getId();
		//5.3 添加规格选项的值
		List<SpecificationOption> options = group.getOptions();
		for (SpecificationOption option : options) {
			//5.3.1 指定规格关联的外键值
			option.setSpecId(id);
			//5.3.2 将规格选项添加到数据库中
			optionMapper.insert(option);
		}

	}

	//6. 修改规格及规格选项
	@Override
	public void update(SpecGroup group) {
		//6.1 修改规格
		Specification specification = new Specification(group.getId(),group.getSpecName());
		specificationMapper.updateByPrimaryKey(specification);

		//6.2 根据外键规格id来删除规格选项
		SpecificationOption option = new SpecificationOption();
		option.setSpecId(specification.getId());
		optionMapper.delete(option);

		//6.3 添加规格选项
		List<SpecificationOption> options = group.getOptions();
		for (SpecificationOption specOption : options) {
			//6.3.1 设置关联外键
			specOption.setSpecId(group.getId());
			//6.3.2 添加规格选项
			optionMapper.insert(specOption);
		}
	}

	//7. 根据规格id删除规格及规格选项列表
	@Override
	public void delete(Long[] ids) {
		//7.1  删除一个或多个规格
		Example example = new Example(Specification.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("id", Arrays.asList(ids));  // xxx where id in (1,2,3)
		specificationMapper.deleteByExample(example);

		//7.2 删除规格选项列表
		Example optionExample = new Example(SpecificationOption.class);
		Example.Criteria cri = optionExample.createCriteria();
		cri.andIn("specId",Arrays.asList(ids));
		optionMapper.deleteByExample(optionExample);

	}
}
